package com.tiancheng.trade.settlement.util;

import com.tiancheng.trade.settlement.enums.BaseEnum;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 枚举工具类
 *
 * @author kellen
 * @date 2024/3/4
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EnumUtil {

    /**
     * 根据code获取枚举对象
     *
     * @param enumClass
     * @param code
     * @param <T>
     * @return
     */
    public static <T extends BaseEnum> T getEnumByCode(Class<T> enumClass, Integer code) {
        for (T each : enumClass.getEnumConstants()) {
            if (each.getCode() == code) {
                return each;
            }
        }
        return null;
    }

    /**
     * 根据code获取枚举对象
     *
     * @param enumClass
     * @param code
     * @param <T>
     * @return
     */
    public static <T extends BaseEnum> T getEnumByCode(Class<T> enumClass, String code) {
        for (T each : enumClass.getEnumConstants()) {
            if (each.getCode().equals(code)) {
                return each;
            }
        }
        return null;
    }

    /**
     * 根据desc获取枚举对象
     *
     * @param enumClass
     * @param desc
     * @param <T>
     * @return
     */
    public static <T extends BaseEnum> T getEnumByDesc(Class<T> enumClass, String desc) {
        for (T each : enumClass.getEnumConstants()) {
            if (each.getDesc().equals(desc)) {
                return each;
            }
        }
        return null;
    }
}
